package com.tanhua.jym.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.common.pojo.BlackList;
import com.tanhua.jym.pojo.AnalysisByDay;
import com.tanhua.jym.pojo.PageResult;
import com.tanhua.jym.pojo.Retention;
import com.tanhua.jym.service.DaySumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/daySum")
public class DaySumController {
    @Autowired
    private DaySumService daySumService;
    public final static String FORMAT_STRING = "yyyy-MM-dd HH:mm:ss";

    public final static String[] REPLACE_STRING = new String[]{"GMT 0800", "GMT+08:00"};

    public final static String SPLIT_STRING = "(中国标准时间)";
    @GetMapping("/day")
    public PageResult findAll(@RequestParam(value = "page", defaultValue = "1") Integer page,
                              @RequestParam(value = "pagesize", defaultValue = "10") Integer pagesize) {

        IPage<AnalysisByDay> infoPage = this.daySumService.findAll(page, pagesize);
        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pagesize);
        long total = infoPage.getTotal();
        pageResult.setCounts(new Long(total).intValue());
        pageResult.setPages(new Long(infoPage.getPages()).intValue());

        List<AnalysisByDay> records = infoPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return pageResult;
        }
        pageResult.setItems(infoPage.getRecords());
        return pageResult;
    }

    @GetMapping("ret")
    public List<Retention> findAll(@RequestParam(value = "start", defaultValue = "2020-05-05") String start,
                                   @RequestParam(value = "end", defaultValue = "2020-06-05") String end) {
        start = this.str2Date(start);
        end = this.str2Date(end);
        List<Retention> retention = this.daySumService.findRetention(start, end);
        return retention;
    }
    public  String str2Date(String dateString) {
        try {
            dateString = dateString.split(Pattern.quote(SPLIT_STRING))[0].replace(REPLACE_STRING[0], REPLACE_STRING[1]);
            SimpleDateFormat sf1 = new SimpleDateFormat("EEE MMM dd yyyy HH:mm:ss z", Locale.US);
            Date date = sf1.parse(dateString);
            SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd");
            String format = sf2.format(date);
            return format;
        } catch (Exception e) {
            throw new RuntimeException("时间转化格式错误" + "[dateString=" + dateString + "]" + "[FORMAT_STRING=" + FORMAT_STRING + "]");
        }
    }

}
